package com.vidmt.api.modules.sys.api;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vidmt.api.common.anno.ApiDoc;
import com.vidmt.api.common.exceptions.BadParamException;
import com.vidmt.api.common.exceptions.UserExistsException;
import com.vidmt.api.common.exceptions.UserNotExistsException;
import com.vidmt.api.common.pojo.ApiResultServer;
import com.vidmt.api.common.web.ApiController;
import com.vidmt.api.modules.sys.Acc;
import com.vidmt.api.modules.sys.Enums.AccType;
import com.vidmt.api.modules.sys.entity.User;
import com.vidmt.api.modules.sys.service.UserService;
import com.vidmt.api.modules.sys.shiro.MyApiToken;
import com.vidmt.api.modules.sys.shiro.MyPrincipal;
import com.vidmt.api.modules.sys.utils.UserStatUtil;
import com.vidmt.api.modules.sys.utils.VUtil;
import com.vidmt.api.modules.sys.utils.VerStatUtil;

@Controller
@ResponseBody
@RequestMapping(value = "/api/1")
public class ApiLoginController extends ApiController {
	private static final Logger log = LoggerFactory.getLogger(ApiLoginController.class);

	@Autowired
	private UserService userService;

	@ApiDoc({ "acctxt",	"格式为acctype:accvalue:pwd,然后进行aes加密，其key=(os+ver).getByte('utf8')。其中的acctype目前为phone/email,根据需要可以进行扩展" })
	@ApiDoc({ "os", "android=a/ios=i" })
	@ApiDoc({ "ver", "对应的版本，例如:10000" })
	@ApiDoc({ "channel", "安卓对应的渠道，允许为空，默认为官方" })
	@RequestMapping("/register.*")
	public ApiResultServer register(String acctxt, String nick, String os, Integer ver,
			@RequestParam(defaultValue = "official") String channel) throws BadParamException, UserExistsException {

		String key = os + ver;
		String[] accarr = VUtil.decodeAcctxt(acctxt, key);

		String pwd = accarr[2];
		Acc acc = new Acc(AccType.valueOf(accarr[0]), accarr[1]);
		if (acc.type() == AccType.uid) {
			throw new BadParamException("不能用uid进行注册");
		}
		if (pwd.length() > 12) {
			return new ApiResultServer(new BadParamException("password"));
		}
		User user = userService.register(acc, pwd, nick);

		UserStatUtil.putReg(os, user.getId(), user.getNick());
		return new ApiResultServer();
	}

	/**
	 * 管理登录
	 */
	@ApiDoc({ "os", "android=a/ios=i" })
	@ApiDoc({ "ver", "对应的版本，例如:10000" })
	@ApiDoc({ "channel", "安卓对应的渠道，允许为空，默认为官方" })
	@ApiDoc({ "返回值", "user对象" })
	@ApiDoc({ "如果多个用户同时登录，会报出异常", "" })
	@RequestMapping(value = "/login.*")
	public ApiResultServer login(String acctxt, String os, Integer ver,
			@RequestParam(defaultValue = "official") String channel) throws UserNotExistsException, BadParamException {

		String key = os + ver;
		String[] accarr = VUtil.decodeAcctxt(acctxt, key);

		String pwd = accarr[2];
		Acc acc = new Acc(AccType.valueOf(accarr[0]), accarr[1]);

		Subject subject = SecurityUtils.getSubject();
		MyApiToken token = new MyApiToken(acc, pwd, false);
		token.setOs(os);
		token.setVer(ver);
		token.setChannel(channel);
		subject.login(token);
		if (subject.isAuthenticated()) {
			MyPrincipal principal = (MyPrincipal) subject.getPrincipal();
			VerStatUtil.put(principal.getUser().getId(), os, ver, channel);
			User user = principal.getUser();
			userService.checkLvlttl(user);
			return new ApiResultServer(user);
		} else {
			throw new UserNotExistsException(acc);
		}
	}

	@RequestMapping(value = "/logout.*")
	public ApiResultServer logout() {
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return new ApiResultServer();
	}
}
